<template>
  <div class="order">
    <nav-bar :isUpper="false">
      <span slot="text">我的订单</span>
    </nav-bar>
    <div class="container">
      <div class="order_list" v-if="orderList">
        <ul>
          <li v-for="(item, index) in orderList" :key="index">
            <!-- <img :src="imageUrl + item.shop_img" alt="" /> -->
            <!-- 测试图片 -->
            <img src="@/assets/images/37.jpg" alt="" />
            <div class="content">
              <header @click="shopInfo(item.shop_id)">
                <div>
                  <p>{{ item.shop_name }}</p>
                  <i class="iconfont icon-zuojiantou icon"></i>
                </div>
                <span>未支付</span>
              </header>
              <div class="food" @click="orderDetail(item.id)">
                <span>
                  {{ item.food[0].food_name }} 等{{ item.food.length }}件商品
                </span>
                <span class="right">¥{{ item.price }}</span>
              </div>
              <button class="btn right" @click="shopInfo(item.shop_id)">
                再来一单
              </button>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="tips" v-if="!(userInfo && userInfo.id)">
      登录后才能查看订单
    </div>
    <main-tab-bar />
    <router-view />
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { imageUrl } from '@/config/env';
import { getOrderList } from '@/network/getData';

export default {
  data() {
    return {
      imageUrl,
      orderList: [],
      isLogin: false
    };
  },
  computed: {
    ...mapState(['userInfo'])
  },
  mounted() {
    if (this.userInfo && this.userInfo.id) {
      this.initData();
    } else {
      this.isLogin = true;
    }
  },
  methods: {
    async initData() {
      const orderList = await getOrderList(this.userInfo.id);
      this.orderList = orderList.data;
    },
    shopInfo(id) {
      this.$router.push({
        path: '/shop',
        query: {
          shopId: id
        }
      });
    },
    orderDetail(id) {
      this.$router.push({
        path: '/order/detail',
        query: {
          orderId: id
        }
      });
    }
  },
  watch: {
    userInfo() {
      this.initData();
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  padding-top: 45px;
}
.order_list {
  li {
    display: flex;
    padding: 10px;
    margin-bottom: 15px;
    background-color: #fff;
    img {
      width: 40px;
      height: 40px;
      margin-right: 10px;
    }
    .content {
      width: 100%;
      header {
        display: flex;
        justify-content: space-between;
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid #ccc;
        p {
          display: inline;
          color: #222;
        }
        span {
          font-size: var(--font-size);
        }
      }
      .food {
        line-height: 35px;
        font-size: var(--font-size);
        border-bottom: 1px solid #ccc;
        .right {
          color: #222;
        }
      }
      .btn {
        margin-top: 10px;
        padding: 2px 5px;
        color: #333;
        font-size: var(--font-size);
        border: 1px solid var(--color-base);
        background-color: transparent;
      }
    }
  }
}
.tips {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
